﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace XBLIGUK2011Talk2.HUD
{
    public class HUDObject
    {
        public BasicHUD Parent;

        public int Top, Left;

        // Click event.
        public OnClickEvent OnClick;

        protected MouseState thisMState;
        protected KeyboardState thiskbState;

        protected MouseState lastMState;
        protected KeyboardState lastkbState;

        public Rectangle bounds;

        public string Name;

        public SpriteBatch spriteBatch
        {
            get { return Parent.spriteBatch; }
        }

        public Game Game
        {
            get { return Parent.Game; }
        }

        public HUDObject(string name, int top, int left)
        {
            Name = name;
            Top = top;
            Left = left;

            bounds = new Rectangle(left, top, 800, 16);
        }

        protected bool clicked = false;
        protected bool dragging = false;
        protected Point delta;
        public virtual void Update(GameTime gameTime)
        {
            thiskbState = Keyboard.GetState();
            thisMState = Mouse.GetState();

            
            if (thisMState.LeftButton == ButtonState.Released && lastMState.LeftButton == ButtonState.Pressed)
            {
                Rectangle t = new Rectangle(thisMState.X, thisMState.Y, 1, 1);
                if (bounds.Intersects(t))
                {                    
                    if (OnClick != null)
                        OnClick(this);
                }
            }


            if (thisMState.LeftButton == ButtonState.Pressed)
            {
                Rectangle t = new Rectangle(thisMState.X, thisMState.Y, 1, 1);
                if (bounds.Intersects(t))
                {
                    clicked = true;
                }
            }

            if (thisMState.LeftButton == ButtonState.Pressed && lastMState.LeftButton == ButtonState.Pressed && clicked && thisMState.X != lastMState.X)
            {
                delta = new Point(thisMState.X - lastMState.X, thisMState.Y - lastMState.Y);
                dragging = true;
            }
            else
                delta = Point.Zero;

            if (thisMState.LeftButton == ButtonState.Released && lastMState.LeftButton == ButtonState.Released)
            {
                clicked = false;
                dragging = false;
            }

            lastkbState = thiskbState;
            lastMState = thisMState;
        }


        public virtual void Draw(GameTime gameTime)
        { }
    }

}
